package com.bean;

import com.annotation.KidLimit;
import com.myenum.Sex;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@KidLimit(value = {"打篮球", "踢足球", "打台球"}, sex = {Sex.MAN, Sex.WOMAN, Sex.GUNSHIP})
public class Kid {
    private String name;
    private String[] hobby;
    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        String[] allowedHobbies = this.getClass().getAnnotation(KidLimit.class).value();

        // 创建一个新的数组来存储符合条件的爱好
        List<String> validHobbies = new ArrayList<>();
        for (String h : hobby) {
            for (String allowedHobby : allowedHobbies) {
                if (h.equals(allowedHobby)) {
                    validHobbies.add(h);
                    break;
                }
            }
        }

        // 将符合条件的爱好转换为数组
        this.hobby = validHobbies.toArray(new String[0]);
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        if (!isValidGender(sex)) {
            throw new IllegalArgumentException("Invalid gender: " + sex);
        }
        this.sex = sex;
    }
    private boolean isValidGender(String gender) {
        Sex[] validGenders = getClass().getAnnotation(KidLimit.class).sex();
        for (Sex validGender : validGenders) {
            if (validGender.toString().equals(gender)) {
                return true;
            }
        }
        return false;
    }
    public Kid() {
    }

    public Kid(String name, String[] hobby, String sex) {
        this.name = name;
        this.hobby = hobby;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "Kid{" +
                "name='" + name + '\'' +
                ", hobby=" + Arrays.toString(hobby) +
                ", sex='" + sex + '\'' +
                '}';
    }
}
